##################################################
# Replication file for Tierra del Fuego Province #
##################################################


rm(list=ls(all=TRUE))
#setwd("")
library(eiPack)
library(coda)
library(foreign)

data=read.csv(file.path(getwd(),"data","TDF_data.csv"))


# Define row and column marginals of the transition matrix. 
GFPV=data$gral_501
GMPF=data$gral_158
GPP=data$gral_174
GO=data$gral_199+data$gral_2+data$gral_3
GN=data$votantes-GFPV-GMPF-GPP-GO


PFPV=data$paso_501
PMPF=data$paso_158
PPP=data$paso_174
PO=data$paso_199+data$paso_2+data$paso_3
PN=data$votantes-PFPV-PMPF-PPP-PO
data1=data.frame(PFPV,PMPF,PPP,PO,PN,GFPV,GMPF,GPP,GO,GN)


# Tunning the EI algorithm
tune.nocov=tuneMD(cbind(GFPV,GMPF,GPP,GO,GN)~cbind(PFPV,PMPF,PPP,PO,PN),
                  data=data1,ntunes=1,totaldraws=200000)


# MCMC
out.nocov=ei.MD.bayes(cbind(GFPV,GMPF,GPP,GO,GN)~cbind(PFPV,PMPF,PPP,PO,PN),
                      covariate=NULL,data=data1,tune.list=tune.nocov,verbose=0,ret.beta="r",thin=80,burnin=100000,ret.mcmc=TRUE) 


# Obtain transition matrix for each mesa: 
source(file.path(getwd(),"Codes","getbetas.R"))
beta11=getbetas(out.nocov,"PFPV","GFPV")
beta12=getbetas(out.nocov,"PFPV","GMPF")
beta13=getbetas(out.nocov,"PFPV","GPP")
beta14=getbetas(out.nocov,"PFPV","GO")
beta15=getbetas(out.nocov,"PFPV","GN")
beta21=getbetas(out.nocov,"PMPF","GFPV")
beta22=getbetas(out.nocov,"PMPF","GMPF")
beta23=getbetas(out.nocov,"PMPF","GPP")
beta24=getbetas(out.nocov,"PMPF","GO")
beta25=getbetas(out.nocov,"PMPF","GN")
beta31=getbetas(out.nocov,"PPP","GFPV")
beta32=getbetas(out.nocov,"PPP","GMPF")
beta33=getbetas(out.nocov,"PPP","GPP")
beta34=getbetas(out.nocov,"PPP","GO")
beta35=getbetas(out.nocov,"PPP","GN")
beta41=getbetas(out.nocov,"PO","GFPV")
beta42=getbetas(out.nocov,"PO","GMPF")
beta43=getbetas(out.nocov,"PO","GPP")
beta44=getbetas(out.nocov,"PO","GO")
beta45=getbetas(out.nocov,"PO","GN")
beta51=getbetas(out.nocov,"PN","GFPV")
beta52=getbetas(out.nocov,"PN","GMPF")
beta53=getbetas(out.nocov,"PN","GPP")
beta54=getbetas(out.nocov,"PN","GO")
beta55=getbetas(out.nocov,"PN","GN")
betaout=data.frame(beta11,beta12,beta13,beta14,beta15,
                   beta21,beta22,beta23,beta24,beta25,
                   beta31,beta32,beta33,beta34,beta35,
                   beta41,beta42,beta43,beta44,beta45,
                   beta51,beta52,beta53,beta54,beta55,
                   data$votantes,data$prov,data$comuna,data$circ,data$mesa)
write.table(betaout,file=file.path(getwd(),"Output","TdF_Mesa.txt"),col.names=NA)


# Obtain aggregate transition matrix. 
source(file.path(getwd(),"Codes","getbetasagr.R"))
beta11=getbetasagr(out.nocov,"PFPV","GFPV")
beta12=getbetasagr(out.nocov,"PFPV","GMPF")
beta13=getbetasagr(out.nocov,"PFPV","GPP")
beta14=getbetasagr(out.nocov,"PFPV","GO")
beta15=getbetasagr(out.nocov,"PFPV","GN")
beta21=getbetasagr(out.nocov,"PMPF","GFPV")
beta22=getbetasagr(out.nocov,"PMPF","GMPF")
beta23=getbetasagr(out.nocov,"PMPF","GPP")
beta24=getbetasagr(out.nocov,"PMPF","GO")
beta25=getbetasagr(out.nocov,"PMPF","GN")
beta31=getbetasagr(out.nocov,"PPP","GFPV")
beta32=getbetasagr(out.nocov,"PPP","GMPF")
beta33=getbetasagr(out.nocov,"PPP","GPP")
beta34=getbetasagr(out.nocov,"PPP","GO")
beta35=getbetasagr(out.nocov,"PPP","GN")
beta41=getbetasagr(out.nocov,"PO","GFPV")
beta42=getbetasagr(out.nocov,"PO","GMPF")
beta43=getbetasagr(out.nocov,"PO","GPP")
beta44=getbetasagr(out.nocov,"PO","GO")
beta45=getbetasagr(out.nocov,"PO","GN")
beta51=getbetasagr(out.nocov,"PN","GFPV")
beta52=getbetasagr(out.nocov,"PN","GMPF")
beta53=getbetasagr(out.nocov,"PN","GPP")
beta54=getbetasagr(out.nocov,"PN","GO")
beta55=getbetasagr(out.nocov,"PN","GN")


betaoutagr=data.frame(beta11,beta12,beta13,beta14,beta15,
                      beta21,beta22,beta23,beta24,beta25,
                      beta31,beta32,beta33,beta34,beta35,
                      beta41,beta42,beta43,beta44,beta45,
                      beta51,beta52,beta53,beta54,beta55,
                      data$votantes,data$prov,data$comuna,data$circ,data$mesa)

betaoutagr=as.vector(betaoutagr[1,1:100])
betaoutagr=matrix(betaoutagr,4)
betaoutagr_coefs=t(matrix(betaoutagr[1,],5))
betaoutagr_sd=t(matrix(betaoutagr[2,],5))
write.table(betaoutagr_coefs,file=file.path(getwd(),"Output","TdFAggregate_Coefs.txt"),col.names=NA)
write.table(betaoutagr_sd,file=file.path(getwd(),"Output","TdFAggregate_SD.txt"),col.names=NA)


#save(out.nocov, file = "TdFMCMC.Rdata") # Saves the MCMC runs


# Geweke Convergence Statistics:
geweke=geweke.diag(out.nocov$draws$Beta)
gewekestats=geweke[[1]]
gewekedf=data.frame(gewekestats)
#write.table(gewekedf,file="TdFgewekelist.txt",col.names=NA)

jpeg(file.path(getwd(),"Output","TdFHistogramGeweke.jpeg"))
hist(gewekestats)
dev.off()
print("Max Geweke")
print(max(gewekestats))
print("Min Geweke")
print(min(gewekestats))
print("Percentage Above 2.35")
print(length(gewekestats[abs(gewekestats)>2.35])/length(gewekestats))
print("Percentage Above 1.96")
print(length(gewekestats[abs(gewekestats)>1.96])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1.5])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1])/length(gewekestats))
print("Percentage Above 0.5")
print(length(gewekestats[abs(gewekestats)>0.5])/length(gewekestats))
